var common_isAndroid = (/android/gi).test(navigator.appVersion);
var common_isIDevice = (/iphone|ipad/gi).test(navigator.appVersion);
var common_isPlaybook = (/playbook/gi).test(navigator.appVersion);
var common_isTouchPad = (/hp-tablet/gi).test(navigator.appVersion);

var common_isMobile = common_isAndroid || common_isIDevice;

var common_isWX = (/MicroMessenger/gi).test(navigator.appVersion);


function func_alert(msg)
{
    alert(msg);
}


//////////////////////////////////////////////////
////// 返回
function func_return()
{
    history.go(-1);
}


//////////////////////////////////////////////////
////// ajax
function func_axios(axios_data)
{
    axios_data.transformRequest = [];


    if (typeof(axios) == 'undefined')
    {
        console.error('not require axios');
        return;
    }
    if (typeof (axios_data.url) == "undefined")
    {
        console.error('url is undefined');
        return;
    }

    if (typeof (axios_data.method) == "undefined")
    {
        axios_data.method = 'get';
    }

    if (typeof (axios_data.timeout) == "undefined")
    {
        //超时时间8秒
        axios_data.timeout = 8000;
    }

    axios_data.responseType = "json";


    if (typeof (axios_data.data) == "undefined")
    {
        axios_data.data = {};
    }

    if (typeof (axios_data.data.__d) == "undefined")
    {
        axios_data.data.__d = (new Date()).valueOf();
    }

    if (typeof (axios_data.headers) == "undefined")
    {
        axios_data.headers = {};
    }

    if (axios_data.method.toLowerCase() == 'get')
    {
        axios_data.params = axios_data.data;
    }
    else if (axios_data.method.toLowerCase() == 'post')
    {
        axios_data.headers = func_extend(axios_data.headers, {
            'Content-Type': 'application/x-www-form-urlencoded'
        });

        var func_change_param = function (data)
        {
            console.log('将数据转换为表单数据');
            let ret = ''
            for (let item in data)
            {
                ret += encodeURIComponent(item) + '=' + encodeURIComponent(data[item]) + '&'
            }
            return ret;
        };

        axios_data.transformRequest.push(func_change_param);
    }


    if (typeof (axios_data.error) == "undefined")
    {
        axios_data.error = function (data)
        {
            console.log('error');
            console.log(data.response);

            var errmsg = data.message;

            func_alert(errmsg, 'error');
        }
    }

    if (typeof (axios_data.before) == "undefined")
    {
        axios_data.before = function (data)
        {
            console.log('before');
            console.log(data);

            return data;
        }
    }

    ////////////////////请求之前触发///////////////////////
    axios_data.transformRequest.push(
        function (data)
        {
            g_vue.loading = true;

            return axios_data.before(data);
        },
    );


    ////////////////////请求完成触发///////////////////////
    axios_data.transformResponse = [
        function (data)
        {
            g_vue.loading = false;

            return data;
        }];


    axios(axios_data)
        .then(axios_data.success)
        .catch(function (obj)
        {
            if (obj.response && obj.response.status == 422)
            {
                //laravel 验证器错误
                var error = obj.response.data.data;
                // console.log(errors);

                func_error_422(error);

                func_alert('提交的数据不正确，请重新输入', 'error');
            }
            else
            {
                axios_data.error(obj);
            }
        });
}


function func_error_422(err)
{
    // console.log(errors);
    // for (var err in errors)
    {
        console.log(err);
        console.log(err.err_field);

        var rules = g_vue.rules[err.err_field];
        console.log(rules);
        if (rules)
        {
            //删除已存在validator规则
            for (var index in rules)
            {
                var r = rules[index];
                if (r['validator'])
                {
                    rules.splice(index, 1);
                }
            }
        }

        g_vue.rules_422[err.err_field] = false;//作用：不让错误出现第二次

        rules.push({
            validator: function (rule, value, callback)
            {
                if (g_vue.rules_422[err.err_field] == false)
                {
                    g_vue.rules_422[err.err_field] = true;
                    callback(new Error(err.err_msg));
                }
                else
                {
                    callback();
                }
            },
        });


        // console.log(rules);
        g_vue.$refs['form'].validateField(err.err_field);
    }
}


//////////////////////////////////////////////////
////// 获取url参数的值
function func_request_get(key)
{
    var request_str = window.location.search;
    request_str = request_str.substr(1);

    var request_data = request_str.split('&');

    var val = "";
    for (var i = 0; i < request_data.length; i++)
    {
        var target_data = request_data[i].split('=');
        if (target_data[0] == key)
        {
            val = target_data[1];

            break;
        }
    }

    return val;
}


//////////////////////////////////////////////////
////// 合并两个json
function func_extend(o, n)
{
    for (var p in n)
    {
        if (n.hasOwnProperty(p) && (!o.hasOwnProperty(p)))
            o[p] = n[p];
    }
    return o;
};